package com.tian.clothing.module;

import com.tian.clothing.R;
import com.tian.clothing.R.layout;
import com.tian.clothing.base.BaseActivity;
import com.tian.clothing.module.fragments.CategoryFragment;
import com.tian.clothing.module.fragments.MainFragment;
import com.tian.clothing.module.fragments.MoreFragment;
import com.tian.clothing.module.fragments.UserFragment;
import com.tian.clothing.widget.TitleBar;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;

/**
 * @ClassName: MainActivity
 * @Description: 主界面
 * @author: dreamfutureba@gmail.com
 * @date: 2013年11月20日 下午4:55:16
 */
public class MainActivity extends BaseActivity {
	private static final String TAG = MainActivity.class.getSimpleName();

	// 定义FragmentTabHost对象
	private FragmentTabHost mTabHost;
	// 定义一个布局
	private LayoutInflater layoutInflater;

	// 定义数组来存放Fragment界面
	private Class fragmentArray[] = { MainFragment.class,
			CategoryFragment.class, UserFragment.class, MoreFragment.class };
	// 定义数组来存放按钮图片
	private int mImageViewArray[] = { R.drawable.main_video_btn,
			R.drawable.main_collect_btn, R.drawable.main_user_btn,
			R.drawable.main_more_btn };

	// Tab选项卡的文字
	private String mTextviewArray[] = { "首页", "类目", "我的", "更多" };

	@Override
	protected void installViews() {
		setContentView(R.layout.activity_main);
		findView();
	}

	/**
	 * 初始化组件
	 */
	private void findView() {
		// 实例化布局对象
		layoutInflater = LayoutInflater.from(this);

		// 实例化TabHost对象，得到TabHost
		mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

		// 得到fragment的个数
		int count = fragmentArray.length;

		for (int i = 0; i < count; i++) {
			// 为每一个Tab按钮设置图标、文字和内容
			TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i])
					.setIndicator(getTabItemView(i));
			// 将Tab按钮添加进Tab选项卡中
			mTabHost.addTab(tabSpec, fragmentArray[i], null);
			// 设置Tab按钮的背景
			mTabHost.getTabWidget().getChildAt(i)
					.setBackgroundResource(R.drawable.selector_tab_background);
		}
	}

	/**
	 * 给Tab按钮设置图标和文字
	 */
	private View getTabItemView(final int index) {
		View view = layoutInflater.inflate(R.layout.tab_item_view, null);

		ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
		imageView.setImageResource(mImageViewArray[index]);

		TextView textView = (TextView) view.findViewById(R.id.textview);
		textView.setText(mTextviewArray[index]);

		return view;
	}
	
	@Override
	protected void registerEvents() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void initTitleBar() {
		mTitleBar.changeStyle(TitleBar.TITLEBAR_STYLE.ONE_TITLE_STYLE);
		super.initTitleBar();
	}

}
